package com.war.service.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.war.dao.system.CustomerDao;
import com.war.pojo.system.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService{
    @Autowired(required = false)
    CustomerDao customerDao;

    @Override
    public HashMap<String, Object> selectAll() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Customer> list = customerDao.selectAll();
        map.put("list",list);
        return map;
    }

    @Override
    public List<Customer> selectName() {
        return customerDao.selectName();
    }

    @Override
    public HashMap<String, Object> select(Customer customer) {
        HashMap<String, Object> map =  new HashMap<String, Object>();
        //1 设置分页参数
        PageHelper.startPage(customer.getPage(),customer.getRow());
        //根据用户选中的查询条件判断用户需要查询的
        List<Customer> list = new ArrayList<>();
        if(customer.getConValue().equals("")){
            list = customerDao.selectAll();
        }else {
            if(customer.getCondition().equals("客户名称")){
                //设置查询条件
                customer.setName(customer.getConValue());
                list.add( customerDao.selectByName(customer));
            }else if(customer.getCondition().equals("法人代表")){
                customer.setLegal_person(customer.getConValue());
                list.add( customerDao.selectByLP(customer));
            }else {
                list = customerDao.selectAll();
            }
        }
        //3.把查询的数据转换成分页对象
        PageInfo<Customer> page = new PageInfo<Customer>(list);

        //获取分页的当前页的集合
        map.put("list",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页数
        map.put("totalPage",page.getPages());
        //上一页
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }

        //下一页
        //保持在最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());

        return map;
    }

    @Override
    public Customer selectByid(Customer customer) {
        return customerDao.selectById(customer);
    }

    @Override
    public String delCus(Customer customer){
        int num = customerDao.delCus(customer);
        if(num>0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String addCus(Customer customer){
        Customer c = customerDao.selectByName(customer);
        if(c != null && c.getId() != customer.getId() ){
            return "当前仓库名已被注册";
        }else {
            int n = customerDao.addCus(customer);
            if(n>0){
                return "添加成功";
            }
            return "添加失败";
        }
    }

    @Override
    public String editCus(Customer customer) {
        Customer c = customerDao.selectByName(customer);
        if(c != null && c.getId() != customer.getId() ){
            return "当前仓库名已被注册";
        }else {
            int n = customerDao.updateCus(customer);
            if(n>0){
                return "修改成功";
            }
            return "修改失败";
        }
    }

}
